# select testFormNumber as '委托单号',sampleModel as '样品型号',sampleNumber as '样品编号',testFormItemName as '委托项名称',
# equipmentName as '设备名称',equipmentNumber as '设备编号',equipmentTWName as '设备台位名称',testCondition as '检测条件',
# varStr1 as '电流',varStr3 as '排气压力',varStr5 as '吸气压力',varStr7 as '排气温度',varStr9 as '吸气温度',varStr11 as '壳体温度',varStr13 as '频率'
# from actualtestvalueg3 where equipmentNumber = 'TA2211080005160024010101' and testFormNumber = 'ST2024099386' and sampleNumber = '2409060741K';

# 定义多个表名和查询条件的键值对
tables_and_conditions = [
    {
        'table': 'actualtestvalueg3',
        'conditions': {
            'testFormNumber': 'ST2024099386',
            'sampleNumber': '2409060741K',
            'equipmentNumber': 'TA2211080005160024010101'
        }
    },
    {
        'table': 'actualtestvaluej3',
        'conditions': {
            'testFormNumber': 'ST20241011803',
            'sampleNumber': '2410160828K',
            'equipmentNumber': 'TA2211080005160003030105'
        }
    },
    # 可以继续添加更多的表和条件
]

# 构造 SQL 查询的函数
def construct_sql(tbl, cons):
    # 选择字段
    fields = [
        "testFormNumber as '委托单号'",
        "sampleModel as '样品型号'",
        "sampleNumber as '样品编号'",
        "testFormItemName as '委托项名称'",
        "equipmentName as '设备名称'",
        "equipmentNumber as '设备编号'",
        "equipmentTWName as '设备台位名称'",
        "testCondition as '检测条件'",
        "varStr1 as '电流'",
        "varStr3 as '排气压力'",
        "varStr5 as '吸气压力'",
        "varStr7 as '排气温度'",
        "varStr9 as '吸气温度'",
        "varStr11 as '壳体温度'",
        "varStr13 as '频率'"
    ]
    
    # 构造 WHERE 条件
    conditions = []
    for key, value in cons.items():
        conditions.append(f"{key} = '{value}'")
    
    # 拼接 SQL 查询
    sql = f"SELECT {', '.join(fields)} FROM {tbl} WHERE {' AND '.join(conditions)};"
    return sql

# 生成所有查询语句并写入文件
import os
current_directory = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(current_directory, 'sql_queries.txt')

with open(file_path, 'w') as file:
    for item in tables_and_conditions:
        tbl = item['table']
        cons = item['conditions']
        sql_query = construct_sql(tbl, cons)
        file.write(sql_query + '\n')

print("所有查询语句已成功写入到 sql_queries.txt 文件中")